<template>
  <div v-loading="pageLoading" class="app-container page-full-screen">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search" />
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          size="small"
          @click="searchList">
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          size="small"
          @click="resetSearch">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="订单编号：">
            <el-input v-model="listQuery.sn" class="input-width" placeholder="订单编号" />
          </el-form-item>
          <el-form-item label="收货人：">
            <el-input v-model="listQuery.receiver_name" class="input-width" placeholder="收货人姓名" />
          </el-form-item>
          <el-form-item label="收货电话：">
            <el-input v-model="listQuery.receiver_phone" class="input-width" placeholder="收货电话" />
          </el-form-item>
          <el-form-item label="提交时间：">
            <el-date-picker
              v-model="listQuery.create_time"
              class="input-width"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="请选择时间" />
          </el-form-item>
          <el-form-item label="订单状态：">
            <el-select v-model="listQuery.status" class="input-width" placeholder="全部" clearable>
              <el-option
                v-for="item in sysDict.order_status"
                :key="item.value"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" />
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <msg-list
        :table-data="showTable"
        :title="title"
        :btntype="btntype"
        :pagesall="parseInt(pagesall)"
        :pages="parseInt(listQuery.page)"
        @getselect="getselect"
        @changepage="changepage1"
        @getid="getid1" />
    </div>
    <order-details :show.sync="detailsData.show" :order="order" :order-history="orderHistory" :order-items="orderItems" />
  </div>
</template>
<script>

import MsgList from '../public/msgList'
import OrderDetails from './components/OrderDetails'
import msgbox from '../../common/msgbox'

import { mapGetters } from 'vuex'

const defaultListQuery = {
  page: 1,
  sn: null,
  receiver_phone: null,
  create_time: null,
  status: null
}
export default {
  name: 'CompanyOrdes',
  components: {
    MsgList,
    OrderDetails
  },
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      pageLoading: true,
      tableData: [],
      title: [
        {
          prop: 'id',
          label: 'id'
        },
        {
          prop: 'sn',
          label: '订单编号'
        },
        {
          prop: 'buy_company_title',
          label: '下单公司'
        },
        {
          prop: 'total_amount',
          label: '订单总金额'
        },
        {
          prop: 'pay_type_show',
          label: '支付方式'
        },
        {
          prop: 'source_type_show',
          label: '订单来源'
        },
        {
          prop: 'status_show',
          label: '订单状态'
        },
        {
          prop: 'create_time',
          label: '提交时间'
        }
      ],
      deletes: {
        ids: []
      },
      btntype: ['查看详情'],
      pagesall: 0,
      detailsData: {
        show: false,
        order: {},
        order_history: [],
        order_items: []
      }
    }
  },
  computed: {
    ...mapGetters(['sysDict', 'sysDictTranslate']),
    order() {
      return this.detailsData.order
    },
    orderItems() {
      return this.detailsData.order_items
    },
    orderHistory() {
      return this.detailsData.order_history
    },
    showTable() {
      return this.tableData.map(item => {
        item.pay_type_show = this.sysDictTranslate.pay_type[item.pay_type]
        item.source_type_show = this.sysDictTranslate.source_type[item.source_type]
        item.status_show = this.sysDictTranslate.order_status[item.status]
        return item
      })
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.pageLoading = true

      const that = this
      // TODO 测试地址
      this.$g.ajax({
        path: 'orders/getList',
        method: 'post',
        data: this.listQuery,
        cbSuccess: function(data) {
          that.tableData = data.memo.list
          that.pagesall = parseInt(data.memo.pageTotal)
        },
        cbError: function(data) {
          const msg = msgbox.get(data.msg)
          that.$message.error(msg)
        },
        cbFinally() {
          that.pageLoading = false
        }
      })
    },
    getid1(val) {
      if (val.btn === '查看详情') {
        this.getmsg = val
        this.getInfo(val.val.id)
      }
    },
    getselect(vals) { // 获取多选id
      this.deletes.ids = []
      for (const i in vals) {
        this.deletes.ids.push(vals[i].id)
      }
    },
    changepage1(page) { // 更改page
      this.listQuery.page = page
      this.search()
    },
    getInfo(id) {
      this.pageLoading = true
      const that = this
      // TODO 测试地址
      this.$g.ajax({
        path: 'orders/getInfo',
        method: 'post',
        data: {
          id
        },
        cbSuccess: function(data) {
          that.detailsData.show = true
          that.detailsData = Object.assign(that.detailsData, data.memo)
        },
        cbError: function(data) {
          const msg = msgbox.get(data.msg)
          that.$message.error(msg)
        },
        cbFinally() {
          that.pageLoading = false
        }
      })
    },
    resetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    searchList() {
      this.listQuery.page = 1
      this.getList()
    }
  }
}
</script>
<style lang="scss" scoped>
.input-width {
  width: 203px;
}

.operate-container {
  margin: 20px 0;
}
</style>
